package com.yc.mvc.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Company 源辰信息
 *
 * @author navy
 * @date 2024/5/4
 * Email haijunzhou@hnit.edu.cn
 */
@Component
public class LoginInterceptor implements HandlerInterceptor {

    @Autowired
    DemoDo demoDo;

    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response,
                             Object handler) throws Exception {
        System.out.println("handler = " + handler); // 控制器
        if (request.getSession().getAttribute("name") == null) {
            String contextPath = request.getServletContext().getContextPath();
            String path = contextPath + "/index.jsp";
            response.sendRedirect(path);
            return false;
        } else {
            return true;
        }
    }
}
